<?php
# =====================================
# 作者：老曹  QQ:1484743
# 版权声明：本程序为个人原创、开源程序，禁止用于任何违法用途，任何违法用途均与作者无关。
# 仅限学习交流，转载请注明出处。如需商用请联系作者。
# =====================================
require_once __DIR__ . '/../core/db.php';
header('Content-Type: application/json');
$data = json_decode(file_get_contents('php://input'), true);
if (!isset($data['name'], $data['ip'], $data['os'])) {
    http_response_code(400);
    echo json_encode(['error' => '参数缺失']);
    exit;
}
$name = $data['name'];
$ip = $data['ip'];
$os = $data['os'];
$token = bin2hex(random_bytes(16));
$db = new DB();
$stmt = $db->prepare('INSERT INTO hosts (name, ip, os, token) VALUES (?, ?, ?, ?)');
$stmt->bind_param('ssss', $name, $ip, $os, $token);
$stmt->execute();
$hostId = $db->insert_id();
$stmt->close();
$db->close();
echo json_encode(['hostId' => $hostId, 'token' => $token]); 